﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;

namespace ZBJF.Warranty.AdminSite.Controllers
{
    public class InfoCollectCategoryController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Index(BaseQueryParams model)
        {
            var total = 0;
            var list = ServiceFactory.InsuranceProductsInfoCollectCategoryServiceInstance.GetPagingList(model, out total).AsNoTracking().ToList().Select(o => new
            {
                o.ID,
                o.InfoCollectCategoryName,
            });
            return Json(new
            {
                sEcho = model.sEcho,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = list
            }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Edit(int id)
        {
            var auditProject = ServiceFactory.InsuranceProductsInfoCollectCategoryServiceInstance.LoadByID(id);
            return View(auditProject);
        }
        [HttpPost]
        public ActionResult Save(InsuranceProductsInfoCollectCategory model)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = model.ID > 0 ? OpreateType.Update : OpreateType.Add
            };
            if (model.ID > 0)
            {
                if (ServiceFactory.InsuranceProductsInfoCollectCategoryServiceInstance.IsExist(model.InfoCollectCategoryName, model.ID))
                {
                    responseResult.Message = "该名称的信息收集项目类别已存在，请修改名字";
                }
                else
                {
                    var entity = ServiceFactory.InsuranceProductsInfoCollectCategoryServiceInstance.LoadByID(model.ID);
                    entity.InfoCollectCategoryName = model.InfoCollectCategoryName;
                    responseResult.Success = ServiceFactory.InsuranceProductsInfoCollectCategoryServiceInstance.UpdateEntities(entity);
                }
            }
            else
            {
                if (
                    ServiceFactory.InsuranceProductsInfoCollectCategoryServiceInstance.IsExist(
                        model.InfoCollectCategoryName))
                {
                    responseResult.Message = "该名称的信息收集项目类别已存在，请修改名字";
                }
                else
                {
                    responseResult.Success = ServiceFactory.InsuranceProductsInfoCollectCategoryServiceInstance.AddEntities(model).ID > 0;
                }
            }
            TempData["ResponseResult"] = responseResult;
            return responseResult.OpreateType == OpreateType.Add && responseResult.Success ? RedirectToAction("Index") : RedirectToAction("Edit", new { id = model.ID });
        }
        [HttpPost]
        public JsonResult Delete(int id)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = OpreateType.Delete
            };
            if (ServiceFactory.InsuranceProductsInfoCollectItemServiceInstance.IsExist("", id))
            {
                responseResult.Message = "删除失败（请先删除该分类下的信息收集项目）";
            }
            else
            {
                responseResult.Success = ServiceFactory.InsuranceProductsInfoCollectCategoryServiceInstance.Delete(id);
            }
            return Json(new { success = responseResult.Success, message = responseResult.Message });
        }
    }
}